package com.linjiahao.serviceChat.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linjiahao.serviceChat.config.MyChannelHandlerPool;
import com.linjiahao.serviceChat.entiy.FriendsAsk;
import com.linjiahao.serviceChat.service.ChatmessageService;
import com.linjiahao.serviceChat.service.FriendsAskService;
import com.linjiahao.serviceChat.vo.AddFriend;
import com.linjiahao.serviceChat.vo.AddMe_Info;
import com.linjiahao.serviceChat.vo.ChatMessage;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Component
public class ChannelReadHandler {

    @Resource
    FriendsAskService friendsAskService;

    @Autowired
    ChatmessageService chatmessageService;

    @Autowired
    RabbitTemplate rabbitTemplate;
    
    public  void addFriend(String msg){
        JSONObject jsonObject=JSONObject.parseObject(msg);
        if(jsonObject.get("add")!=null){
            Channel channel = MyChannelHandlerPool.channelGroup.find(MyChannelHandlerPool.channelIdMap.get(jsonObject.getString("her_id")));
            if(channel==null){
                //将msg持久化
                String own=jsonObject.getString("own_id");
                String her=jsonObject.getString("her_id");
                FriendsAsk one = friendsAskService.getOne(new QueryWrapper<FriendsAsk>().eq("send_id", own).eq("get_id", her));
                FriendsAsk friendsAsk = new FriendsAsk();
                friendsAsk.setSendId(own);
                friendsAsk.setGetId(jsonObject.getString("her_id"));
                boolean save = friendsAskService.saveOne(friendsAsk);
                rabbitTemplate.convertAndSend("exchange", "topic.add-friend", her);
            }else {
                //直接转发给用户
                String own=jsonObject.getString("own_id");
                String her=jsonObject.getString("her_id");
                FriendsAsk one = friendsAskService.getOne(new QueryWrapper<FriendsAsk>().eq("send_id", own).eq("get_id", her));
                if(one==null){
                    AddFriend addFriend = new AddFriend();
                    addFriend.setOwn_Id(own);
                    Object o = JSONObject.toJSON(addFriend);
                    String m=o.toString();
                    channel.writeAndFlush(new TextWebSocketFrame(m));
                }
                FriendsAsk friendsAsk = new FriendsAsk();
                friendsAsk.setSendId(own);
                friendsAsk.setGetId(jsonObject.getString("her_id"));
                boolean save = friendsAskService.saveOne(friendsAsk);
                List<AddMe_Info> addMeByList = friendsAskService.getAddMeByList(friendsAsk.getGetId());
                JSONArray jsonArray=new JSONArray(Collections.singletonList(addMeByList));
                channel.writeAndFlush(new TextWebSocketFrame(jsonArray.toString()));
            }
        }
    }

    public void sendMessage(String text) {
        JSONObject jsonObject=JSONObject.parseObject(text);
        if(jsonObject.get("her_id")!=null){
            Channel channel = MyChannelHandlerPool.channelGroup.find(MyChannelHandlerPool.channelIdMap.get(jsonObject.getString("her_id")));
            if(channel==null){
               //聊天消息持久化
                boolean b = chatmessageService.saveMessage(jsonObject);
                rabbitTemplate.convertAndSend("exchange", "topic.add-friend", jsonObject.getString("her_id")+"|"+jsonObject.getString("myid"));
            }else {

                ChatMessage chatMessage = new ChatMessage();
                chatMessage.setData(jsonObject.getString("date"));
                chatMessage.setText(jsonObject.getString("text"));
                chatMessage.setImg(jsonObject.getString("img"));
                chatMessage.setMine("false");
                chatMessage.setName(jsonObject.getString("name"));
                chatMessage.setMyid(jsonObject.getString("myid"));
                Object o = JSONObject.toJSON(chatMessage);
                String m=o.toString();
                System.out.println(m);
                channel.writeAndFlush(new TextWebSocketFrame(m));

                //聊天信息保存
                boolean b = chatmessageService.saveMessage(jsonObject);
            }
        }

    }
}
